/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution 
{
public:
    TreeNode* pruneTree(TreeNode* root) 
    {
        if(root==nullptr) return nullptr;
        if(root->left==nullptr&&root->right==nullptr)
        {
            if(root->val==0) return nullptr;
            else return root;
        }
        TreeNode* l= pruneTree(root->left);
        TreeNode* r= pruneTree(root->right);
        //此时判断左右子树
        root->left=l;
        root->right=r;
        //值为1直接返回
        if(root->val==1) return root;
        //值为0如果它不是叶子节点不删除
        else 
        {
            if(root->left==nullptr&&root->right==nullptr) return nullptr;
            else return root;
        }
    }
};